package com.woniuxy.his.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.his.entity.Medicine;
import com.woniuxy.his.mapper.MedicineMapper;
import com.woniuxy.his.service.IMedicineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.his.vo.AccountMedVo;
import com.woniuxy.his.vo.AccountMedicineVo;
import com.woniuxy.his.vo.AllMedicine;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements IMedicineService {

    @Resource
    private MedicineMapper medicineMapper;

    //模糊查询药品
    @Override
    public IPage<Medicine> findMedicineLikeName(String productName, Integer current, Integer pageSize) {
        IPage page =new Page(current,pageSize);
        IPage medicines = medicineMapper.findByPage(page, productName);
        return medicines;
    }


    //项目划价后,传递项目信息,写入检查表和检查详情表
    @Override
    public Integer accountMedicine(AccountMedicineVo accountMedicineVo) {
        //获取病例ID
        Integer caseId = medicineMapper.getCaseId(accountMedicineVo.getGuaId());

        //插入检查表
        Integer integer = medicineMapper.insertMedicine(accountMedicineVo.getTotalMedicine(), caseId, accountMedicineVo.getDoctorId());

        //添加处方详情表

        //获取药品ID
        ArrayList<AccountMedVo> tableMedicines = accountMedicineVo.getTableMedicine();

        ArrayList<String> medicineNames = new ArrayList<>();
        for (AccountMedVo amv:tableMedicines){
            medicineNames.add(amv.getProductName());
        }
        List<Integer> medicineIds = medicineMapper.getMedicineId(medicineNames);

        //获取处方ID
        Integer prescriptionId = medicineMapper.getPrescriptionId(caseId);

        int a=0;
        //插入处方详情表
        for (AccountMedVo amv:tableMedicines){
            medicineMapper.insertHisPresDetail(medicineIds.get(a),amv.getNum4(),prescriptionId);
            a++;
        }

        return a;
    }

    //获取药品库所有的数据信息
    @Override
    public IPage getAllMedicine(int current,int pageSize) {
        IPage page = new Page(current, pageSize);
        IPage iPage = medicineMapper.getAllMedicine(page);
        return iPage;
    }

}
